import callApi from '../utils/request';
import {
  API_COUPON_QR_STATUS,
  API_PAY_CODE,
  API_QUERY_ORDER_BY_CODE,
} from '../constants/api';

export function queryOrderByCode(params) {
  return callApi({
    api: API_QUERY_ORDER_BY_CODE,
    params,
  });
}

export function genPayCode(params) {
  return callApi({
    api: API_PAY_CODE,
    params,
  });
}

export function queryCouponQRStatus(params) {
  return callApi({
    api: API_COUPON_QR_STATUS,
    params,
  });
}

export function genQrcode(code) {
  return import('qrcode').then((module) => {
    const QRCode = module.default;
    return QRCode.toDataURL(code, {
      errorCorrectionLevel: 'H',
      width: 400,
      height: 400,
    });
  }).catch(e => console.error('加载qrcode库失败', e));
}

export function genBarcode(code) {
  return import('jsbarcode/dist/barcodes/JsBarcode.code128.min.js').then(() => {
    const canvas = document.createElement('canvas');
    window.JsBarcode(canvas, code, {
      format: 'CODE128',
      displayValue: false,
      height: 154,
      width: 4,
      margin: 0,
    });
    return canvas.toDataURL('image/png');
  }).catch(e => console.error('加载barcode库失败', e));
}
